package me.kaede.sample.adapter;

import me.kaede.sample.ResourceTable;
import me.kaede.tagview.Tag;
import me.kaede.tagview.TagView;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

/**
 * RecyclerViewAdapter
 *
 * @since 2021-06-07
 */
public class RecyclerViewAdapter extends BaseItemProvider {
    private String[] mListDataList;
    private Context mContext;

    private void setListDataList(String[] listDataList){
        if(listDataList != null){
            this.mListDataList = listDataList;
        }
    }

    /**
     * RecyclerViewAdapter
     *
     * @param context
     * @param listDataList
     */
    public RecyclerViewAdapter(Context context, String[] listDataList) {
        this.mContext = context;
        setListDataList(listDataList);
    }

    /**
     * getCount
     *
     * @return int
     */
    @Override
    public int getCount() {
        return mListDataList.length;
    }

    /**
     * getItem
     *
     * @param position
     * @return String
     */
    @Override
    public String getItem(int position) {
        String textValue = null;
        if (mListDataList != null && position >= 0 && position < mListDataList.length) {
            textValue = mListDataList[position];
            return textValue;
        }
        return textValue;
    }

    /**
     * getItemId
     *
     * @param position
     * @return long
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * getComponent
     *
     * @param position
     * @param convertComponent
     * @param componentContainer
     * @return Component
     */
    @Override
    public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {
        final Component cpt;
        if (position != mListDataList.length - 1) {
            cpt = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_item_list_text, null, false);
            Text tv = (Text) cpt.findComponentById(ResourceTable.Id_tv);
            tv.setText(mListDataList[position]);
        } else {
            cpt = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_item_list_tag, null, false);
            TagView tagviewItemList = (TagView) cpt.findComponentById(ResourceTable.Id_tagview_item_list);
            List<Tag> tagList = new ArrayList<>();
            tagList.add(new Tag("FOOTER 1"));
            tagList.add(new Tag("FOOTER 2"));
            tagviewItemList.removeAllTags();
            tagviewItemList.addTags(tagList);
        }
        return cpt;
    }
}
